package com.example.wallpaperapp.ui.fragment;

import android.content.Context;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import com.example.wallpaperapp.R;
import com.example.wallpaperapp.adapter.FragmentAdapte;
import com.example.wallpaperapp.adapter.ImageNetAdapter;
import com.example.wallpaperapp.base.BaseFragment;
import com.example.wallpaperapp.bean.DataBean;
import com.example.wallpaperapp.ui.activity.ChoicenessActivity;
import com.example.wallpaperapp.ui.activity.ClassifyActivity;
import com.example.wallpaperapp.ui.activity.CompilationsActivity;
import com.example.wallpaperapp.ui.activity.KnowledgeActivity;
import com.example.wallpaperapp.ui.activity.ListActivity;
import com.example.wallpaperapp.ui.activity.RankingActivity;
import com.example.wallpaperapp.utils.IntentUtil;
import com.google.android.material.tabs.TabLayout;
import com.youth.banner.Banner;
import com.youth.banner.indicator.CircleIndicator;
import com.youth.banner.transformer.AlphaPageTransformer;

import java.util.ArrayList;
import java.util.List;

public class HomeFragment extends BaseFragment {
    private ViewPager viewPager;
    private TabLayout tabLayout;
    private Banner banner;
    private List<Fragment> list;
    private LinearLayout linearLayout0,linearLayout1,linearLayout2,linearLayout3,linearLayout4,linearLayout5;



    @Override
    protected void initView(View view) {
        viewPager  = view.findViewById(R.id.home_fragment_viewpager);
        tabLayout = view.findViewById(R.id.tablayout);
        banner = view.findViewById(R.id.banner);
        linearLayout0 = view.findViewById(R.id.home_linear_choiceness);
        linearLayout1 = view.findViewById(R.id.home_linear_compilations);
        linearLayout2 = view.findViewById(R.id.home_linear_knowledge);
        linearLayout3 = view.findViewById(R.id.home_linear_ranking);
        linearLayout4 = view.findViewById(R.id.home_linear_classify);
        setOnClick();
        bannerDatas();
    }
    protected void setOnClick() {
        linearLayout0.setOnClickListener(this);
        linearLayout1.setOnClickListener(this);
        linearLayout2.setOnClickListener(this);
        linearLayout3.setOnClickListener(this);
        linearLayout4.setOnClickListener(this);

    }

    //设置轮播图数据
    private void bannerDatas() {
        banner.setAdapter(new ImageNetAdapter(DataBean.getTestData3()));
        //设置指示器
        banner.setIndicator(new CircleIndicator(getActivity()));
        //添加画廊效果(可以和其他PageTransformer组合使用，比如AlphaPageTransformer，注意但和其他带有缩放的PageTransformer会显示冲突)
        //banner.setBannerGalleryEffect(18,10);
        //添加透明效果(画廊配合透明效果更棒)
       // banner.addPageTransformer(new AlphaPageTransformer());
    }
    @Override
    protected int getLayoutId() {
        return R.layout.fragment_home;
    }
    @Override
    protected void initData(Context context) {

        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                View view = tab.getCustomView();
                if (null == view) {
                    tab.setCustomView(R.layout.tab_item_text);
                }
                TextView textView = tab.getCustomView().findViewById(android.R.id.text1);
                textView.setTextAppearance(getActivity(), R.style.TabLayoutTextSize);
            }
            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                View view = tab.getCustomView();
                if (null == view) {
                    tab.setCustomView(R.layout.tab_item_text);
                }
                TextView textView = tab.getCustomView().findViewById(android.R.id.text1);
                textView.setTextAppearance(getActivity(), R.style.TabLayoutTextSize_two);
            }
            @Override
            public void onTabReselected(TabLayout.Tab tab) {
            }
        });
        list = new ArrayList<>();
        list.add(new HottestFragment());
        list.add(new NewestFragment());
        //Fragment嵌套Fragment 用getChildFragmentManager
        FragmentAdapte adapter = new FragmentAdapte(list,getChildFragmentManager());
        viewPager.setAdapter(adapter);
        //联动
        tabLayout.setupWithViewPager(viewPager);
        tabLayout.getTabAt(0).setText("最热");
        tabLayout.getTabAt(1).setText("最新");

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.home_linear_choiceness:
                IntentUtil.startActivity(ChoicenessActivity.class,getActivity());
                break;
            case R.id.home_linear_compilations:
                IntentUtil.startActivity(CompilationsActivity.class,getActivity());
                break;
            case R.id.home_linear_knowledge:
                IntentUtil.startActivity(KnowledgeActivity.class,getActivity());
                break;
            case R.id.home_linear_ranking:
                IntentUtil.startActivity(ListActivity.class,getActivity());
                break;
            case R.id.home_linear_classify:
                IntentUtil.startActivity(ClassifyActivity.class,getActivity());
                break;

        }
    }
}